//
//  UIViewTest.swift
//  EasySwift
//
//  Created by 王隆帅 on 2017/6/10.
//  Copyright © 2017年 王隆帅. All rights reserved.
//

import UIKit

class UIViewTest: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white;
        // Do any additional setup after loading the view.
        
        self.testTranslatesAutoresizingMaskIntoConstraints();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    var layoutView = UIView.init();
    
    func testRequiresConstraintBasedLayout() {
        
        self.layoutView.backgroundColor = UIColor.brown;
        self.view.addSubview(self.layoutView);
    }
    
    override func updateViewConstraints() {
        
        self.view.addConstraint(NSLayoutConstraint.init(item: self.layoutView, attribute:.width , relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 40));
        self.view.addConstraint(NSLayoutConstraint.init(item:  self.layoutView, attribute:.height , relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 40));
        self.view.addConstraint(NSLayoutConstraint.init(item:  self.layoutView, attribute:.left , relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1.0, constant: 40));
        self.view.addConstraint(NSLayoutConstraint.init(item:  self.layoutView, attribute:.top , relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 100));
    }
    
    func testTranslatesAutoresizingMaskIntoConstraints() {
        
        let view1 = UIView.init();
        view1.backgroundColor = UIColor.blue;
        self.view.addSubview(view1);
        view1.translatesAutoresizingMaskIntoConstraints = false;
    }
    
    // layoutSubviews
    
    var testBottomView = UIScrollView.init();
    var testTopView = UIView.init();

    func testLayoutSubviews() {
        
        print("initViewWithFrame ------- ");
        self.testBottomView = TestLayoutScrollView.init(frame: CGRect.init(x: 100, y: 100, width: 200, height: 200));
        self.testBottomView.backgroundColor = UIColor.green;
        self.testBottomView.contentSize = CGSize.init(width: 200, height: 800);
        print("addSubview ------- ");
        self.view.addSubview(self.testBottomView);
        
        self.testTopView = UIView.init(frame: CGRect.init(x: 10, y: 10, width: 50, height: 50));
        self.testTopView.backgroundColor = UIColor.blue;
        self.testBottomView.addSubview(self.testTopView);
        
        let btn1 = UIButton.init(type: .custom);
        btn1.addTarget(self, action: #selector(changeBottomView), for: .touchUpInside);
        btn1.backgroundColor = UIColor.cyan;
        btn1.frame = CGRect.init(x: 20, y: 500, width: 50, height: 50);
        self.view.addSubview(btn1);
        
        let btn2 = UIButton.init(type: .custom);
        btn2.addTarget(self, action: #selector(changeTopView), for: .touchUpInside);
        btn2.backgroundColor = UIColor.brown;
        btn2.frame = CGRect.init(x: 100, y: 500, width: 50, height: 50);
        self.view.addSubview(btn2);
    }
    
    func changeBottomView() {
        
//        print("changeBottomView ------- ");
//        self.testBottomView.frame = CGRect.init(x: 120, y: 120, width: 210, height: 210);
        
//        print("setNeedsLayout ------- ");
//        self.testBottomView.setNeedsLayout();
        
        print("layoutIfNeeded ------- ");
        self.testBottomView.layoutIfNeeded();
        
        print("完毕 ------- ");
    }
    
    func changeTopView() {
        
        print("changeTopView ------- ");
        self.testTopView.bounds = CGRect.init(x: 0, y: 0, width: 100, height: 100);
    }
    
    // sizeThatFits && sizeToFit
    
    func testSizeFits() {
        
        let sizeLabel = UILabel.init(frame: CGRect.init(x: 20, y: 100, width: 0, height: 0));
        sizeLabel.font = UIFont.systemFont(ofSize: 20);
        sizeLabel.text = "王隆帅的简书 王隆帅的博客 王隆帅!!!";
        self.view.addSubview(sizeLabel);
        
        let fitSize = sizeLabel.sizeThatFits(CGSize.zero);
        print("sizeThatFits ------- \n" + String(describing: fitSize));
        print("sizeThatFits 后 Label的尺寸 ------- \n" + String(describing: sizeLabel.frame.size));
        
        sizeLabel.sizeToFit();
        print("sizeToFit 后 Label的尺寸 ------- \n" + String(describing: sizeLabel.frame.size));
    }
    
    // autoresizingMask && autoresizesSubviews
    
    var firstView = UIView.init();
    
    func testAutoresizingMask() {
        
        self.firstView.frame = CGRect.init(x: 20, y: 80, width: 200, height: 200);
        self.firstView.backgroundColor = UIColor.red;
        
        self.view.addSubview(self.firstView);
        
        let secondView = UIView.init(frame: CGRect.init(x: 10, y: 10, width: 180, height: 20));
        secondView.backgroundColor = UIColor.brown;
        secondView.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin];
        self.firstView.addSubview(secondView);
        
        let thirdView = UIView.init(frame: CGRect.init(x: 10, y: 40, width: 180, height: 20));
        thirdView.backgroundColor = UIColor.cyan;
        thirdView.autoresizingMask = .flexibleLeftMargin;
        self.firstView.addSubview(thirdView);
        
        let fourthView = UIView.init(frame: CGRect.init(x: 10, y: 70, width: 180, height: 20));
        fourthView.backgroundColor = UIColor.blue;
        fourthView.autoresizingMask = .flexibleRightMargin;
        self.firstView.addSubview(fourthView);
        
        let fifthView = UIView.init(frame: CGRect.init(x: 10, y: 110, width: 180, height: 50));
        fifthView.backgroundColor = UIColor.yellow;
        fifthView.autoresizingMask = [.flexibleTopMargin, .flexibleHeight];
        self.firstView.addSubview(fifthView);
        
        let changeBtn = UIButton.init(type: .custom);
        changeBtn.setTitle("更改frame", for: .normal);
        changeBtn.frame = CGRect.init(x: 20, y: 500, width: 120, height: 40);
        changeBtn.backgroundColor = UIColor.green;
        changeBtn.addTarget(self, action: #selector(changeAutoresizingMaskFrame), for: .touchUpInside);
        self.view.addSubview(changeBtn);
        
        self.firstView.autoresizesSubviews = false;
    }
    
    func changeAutoresizingMaskFrame() {
        
        let framesArray = [220,250,270,300,320];
        
        let index = Int(arc4random() % 5);
        
        self.firstView.frame = CGRect.init(x: 20, y: 80, width: framesArray[index], height: framesArray[index]);
    }
    
    // 测试 视图的层次结构
    func testHierarchicalStructure() {
   
        let view1 = self.createView(tag: 0);
        self.view.addSubview(view1);

        let view2 = self.createView(tag: 1);
        self.view.addSubview(view2);
  
        let view3 = self.createView(tag: 2);
        self.view.addSubview(view3);
        
        print("superView:\n" + String(describing: view1.superview));
        self.printSubviews(name: "subviews");

        let view101 = self.createView(tag: 100);
        UIApplication.shared.keyWindow?.addSubview(view101);
        print("\nwindow2:\n" + String(describing: view101.window))
        
        view1.removeFromSuperview();
        self.printSubviews(name: "removeFromSuperview");
        
        self.view.insertSubview(view1, at: 1);
        self.printSubviews(name: "insertSubviewAtIndex");

        self.view.exchangeSubview(at: 0, withSubviewAt: 1);
        self.printSubviews(name: "exchangeSubview");

        let view4 = self.createView(tag: 3);
        self.view.addSubview(view4);
        self.printSubviews(name: "addSubview");
        
        
        let view5 = self.createView(tag: 4);
        self.view.insertSubview(view5, aboveSubview: view3);
        self.printSubviews(name: "insertSubview:aboveSubview");

        
        let view6 = self.createView(tag: 5);
        self.view.insertSubview(view6, belowSubview: view3);
        self.printSubviews(name: "insertSubview:belowSubview");

        self.view.bringSubview(toFront: view2);
        self.printSubviews(name: "bringSubview:toFront");
        
        self.view.sendSubview(toBack: view2);
        self.printSubviews(name: "sendSubview:Toback");
        
        print("\nview2 && view3  isDescendant: " + String(view2.isDescendant(of: view3)));
        
        print("view2 && self.view isDescendant: " + String(view2.isDescendant(of: self.view)));
        
        print("self.view && view2 isDescendant: " + String(self.view.isDescendant(of: view2)));
        
        print("view2 && view2 isDescendant: " + String(view2.isDescendant(of: view2)));

    }
    
    func createView(tag: NSInteger) -> UIView {
        
        let view = UIView.init(frame: CGRect.init(x: 100, y: 100, width: 200, height: 200));
        view.tag = tag;
        
        return view;
    }
    
    func printSubviews(name: String) {
        
        print("\n\(name):\n" + String(describing: self.view.subviews.map { (view: UIView) -> NSInteger in return view.tag; }));
    }
    
    // 测试 frame && bounds && center
    func testFrame() {
        
        let view = UIView.init(frame: CGRect.init(x: 100, y: 200, width: 50, height: 50));
        self.view.addSubview(view);
        
        print(view.frame);
        print(view.bounds);
        print(view.center);
    }
    
    // 测试 isExclusiveTouch
    func testIsExclusiveTouch() {
        
        let btn1 = UIButton.init(frame: CGRect.init(x: 100, y: 100, width: 100, height: 100))
        btn1.backgroundColor = UIColor.red;
//        btn1.isExclusiveTouch = true;
        btn1.addTarget(self, action: #selector(touchClickBtn), for: .touchUpInside);
        self.view.addSubview(btn1);
        
        let btn2 = UIButton.init(frame: CGRect.init(x: 220, y: 100, width: 100, height: 100))
        btn2.backgroundColor = UIColor.blue;
//        btn2.isExclusiveTouch = true;
        btn2.addTarget(self, action: #selector(touchClickBtn), for: .touchUpInside);
        self.view.addSubview(btn2);
    }
    
    func touchClickBtn() {
        
        print("btn 被点击了！！");
    }
    
    // 测试 isMultipleTouchEnabled
    var touchNums = 0;
    func testIsMultipleTouchEnabled() {
        
        self.view.isMultipleTouchEnabled = false;
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        touchNums = touchNums + touches.count;
        print(touchNums);
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        touchNums = touchNums - touches.count;
        print(touchNums);
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        touchNums = touchNums - touches.count;
        print(touchNums);
    }
    
    // 测试 layer
    func testLayer() {
        
        let layer = CALayer.init();
        layer.bounds = CGRect.init(x: 0, y: 0, width: 100, height: 100);
        layer.position = CGPoint.init(x: 100, y: 200);
        layer.contents = UIImage.init(named: "imageToColor")?.cgImage;
        layer.cornerRadius = 10.0;
        layer.masksToBounds = true;
        
        self.view.layer.addSublayer(layer);
    }
    
    // 测试 mask
    func testMask() {
        
        let view1 = UIView.init(frame: CGRect.init(x: 20, y: 80, width: 80, height: 80));
        view1.backgroundColor = UIColor.blue;
        
        let maskView1 = UIView.init(frame: view1.bounds);
        maskView1.backgroundColor = UIColor.red;
        maskView1.alpha = 0.1;

        view1.mask = maskView1;
        self.view.addSubview(view1);
        
        
        let view2 = UIView.init(frame: CGRect.init(x: 120, y: 80, width: 80, height: 80));
        view2.backgroundColor = UIColor.blue;
        
        let maskView2 = UIView.init(frame: view2.bounds);
        maskView2.backgroundColor = UIColor.green;
        maskView2.alpha = 0.5;
        
        view2.mask = maskView2;
        self.view.addSubview(view2);

        
        let view3 = UIView.init(frame: CGRect.init(x: 220, y: 80, width: 80, height: 80));
        view3.backgroundColor = UIColor.blue;
        
        let maskView3 = UIView.init(frame: view3.bounds);
        maskView3.backgroundColor = UIColor.brown;
        maskView3.alpha = 1.0;
        
        view3.mask = maskView3;
        self.view.addSubview(view3);
    }
    
    // 测试 clipsToBounds
    func testClipsToBounds() {
        
        // clipsToBounds false
        let clipsView1_down = self.getNewTestView(index_i: 0, index_j: 0);
        clipsView1_down.clipsToBounds = false;

        let clipsView1_up = self.getNewTestView(index_i: 0, index_j: 1);
        clipsView1_up.backgroundColor = #colorLiteral(red: 0.1294117719, green: 0.2156862766, blue: 0.06666667014, alpha: 1);
        clipsView1_up.center = CGPoint.init(x: 25, y: 50);
        
        clipsView1_down.addSubview(clipsView1_up);
        
        // clipsToBounds true
        let clipsView2_down = self.getNewTestView(index_i: 1, index_j: 0);
        clipsView2_down.clipsToBounds = true;
        
        let clipsView2_up = self.getNewTestView(index_i: 1, index_j: 1);
        clipsView2_up.backgroundColor = #colorLiteral(red: 0.1294117719, green: 0.2156862766, blue: 0.06666667014, alpha: 1);
        clipsView2_up.center = CGPoint.init(x: 25, y: 50);
        
        clipsView2_down.addSubview(clipsView2_up);
    }
    
    
    // 测试 tintColor
    func testTintColor() {
        
        let tintColorView = TIntColorTestView.init(frame: self.view.bounds);
        self.view.addSubview(tintColorView);
    }
    
    // test alpha && isOpaque && isHidden
    
    func testalpha_isOpaque_isHidden() {
        
        // test alpha
        let leftAlphaView = self.getNewTestView(index_i: 0, index_j: 0);
        leftAlphaView.alpha = 0.0;
        
        let centerAlphaView = self.getNewTestView(index_i: 1, index_j: 0);
        centerAlphaView.alpha = 0.5;
        
        let rightAlphaView = self.getNewTestView(index_i: 2, index_j: 0);
        rightAlphaView.alpha = 1.0;
        
        // test isHidden
        
        let leftIsHiddenView = self.getNewTestView(index_i: 0, index_j: 1);
        leftIsHiddenView.isHidden = true;
        
        let rightIsHiddenView = self.getNewTestView(index_i: 1, index_j: 1);
        rightIsHiddenView.isHidden = false;
        
        // test isOpaque
        
        let leftDownOpaqueView = self.getNewTestView(index_i: 0, index_j: 2);
        let leftUpOpaqueView = self.getNewTestView(index_i: 0, index_j: 3);
        leftUpOpaqueView.alpha = 1.0;
        leftUpOpaqueView.isOpaque = true;
        leftUpOpaqueView.backgroundColor = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1);
        leftUpOpaqueView.center = CGPoint.init(x: leftDownOpaqueView.center.x, y: leftDownOpaqueView.center.y + 25);
        
        let rightDownOpaqueView = self.getNewTestView(index_i: 1, index_j: 2);
        let rightUpOpaqueView = self.getNewTestView(index_i: 1, index_j: 3);
        rightUpOpaqueView.alpha = 0.5;
        rightUpOpaqueView.isOpaque = false;
        rightUpOpaqueView.backgroundColor = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)
        rightUpOpaqueView.center = CGPoint.init(x: rightDownOpaqueView.center.x, y: rightDownOpaqueView.center.y + 25);
    }
    
    func getNewTestView(index_i: NSInteger, index_j: NSInteger) -> UIView {
        
        let testView = UIView.init(frame: CGRect.init(x: 30 + index_i * 80, y: 80 + index_j * 80, width: 50, height: 50));
        testView.backgroundColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1);
        
        self.view.addSubview(testView);
        
        return testView;
    }
    
    // test view 纯代码初始化
    func testViewInit() {
        
        let testView = TestInitView.init(frame: CGRect.init(x: 100, y: 100, width: 100, height: 100));
        testView.backgroundColor = #colorLiteral(red: 0.1215686277, green: 0.01176470611, blue: 0.4235294163, alpha: 1);
        
        self.view.addSubview(testView);
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
